package com.example.controller;

import com.example.model.UserInfo;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description:
 * User: 阿宾
 * Date: 2025-05-05
 * Time: 20:08
 */
@RequestMapping("/user")
@RestController
public class UserController {
    //事务管理器，用来开启事务，提交或回滚事务
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;
    //定义事务属性
    @Autowired
    private TransactionDefinition transactionDefinition;

    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    public Boolean register(UserInfo userInfo){
        //开启事务
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        //用户注册
        Integer result = userService.registerUser(userInfo);
        System.out.println("注册成功,影响行数:"+result);

        //提交事务
        dataSourceTransactionManager.commit(transactionStatus);
//        //回滚事务
//        dataSourceTransactionManager.rollback(transactionStatus);
        return true;
    }
}
